package com.guocheng.modules.dictionarie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guocheng.entity.AjaxResult;
import com.guocheng.modules.dictionarie.model.Dictionarie;
import com.guocheng.modules.dictionarie.model.DictionariesPO;
import com.guocheng.modules.dictionarie.service.DictionariesService;
import com.guocheng.modules.dictionarie.utils.DictionarieUtils;
import com.guocheng.modules.dictionarie.utils.Dictionaries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/dictionaries")
public class DictionariesController {

    @Autowired
    private DictionariesService dictionariesService;

    /**
     * 重新加载数据字典接口
     * @return
     */
    @GetMapping(value = "/init",name = "重新加载数据字典接口")
    @ResponseBody
    public AjaxResult init(){
        boolean init = Dictionaries.init();
        return init?AjaxResult.ok():AjaxResult.error();
    }

    @GetMapping(value = "/type/{type}",name = "根据名称获取数据字典值")
    @ResponseBody
    public AjaxResult get(@PathVariable("type")String type){
        List<Dictionarie> dictionaries = Dictionaries.getDictionaries(type);
        return AjaxResult.ok().data(dictionaries);
    }

    /**
     * 数据字典页面
     * @return
     */
    @GetMapping("/index")
    public String index(){
        return "dictionaries/list";
    }

    /**
     * 获取数据字典接口
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult list(){
        List<DictionariesPO> dictionaries = dictionariesService.selectList();
        return AjaxResult.ok().data(dictionaries);
    }

    /**
     * 打开信息页面
     * @return
     */
    @GetMapping("/show")
    public String show(@RequestParam(value = "id",required = false)Integer id, Model model){
        if(id!=null){
            DictionariesPO dictionaries = dictionariesService.selectById(id);
            model.addAttribute("dictionaries",dictionaries);
            if(!dictionaries.getParentId().equals(0)){
                List<Dictionarie> title = Dictionaries.getDictionaries("title");
                for (Dictionarie dictionarie : title) {
                    if(dictionarie.getId().equals(dictionaries.getParentId()));
                    model.addAttribute("parentName",dictionarie.getName());  //父名称
                    break;
                }
            }
        }
        return "dictionaries/show";
    }

    /**
     * 保存提交
     * @param dictionariesPO
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(DictionariesPO dictionariesPO){
        DictionariesPO init = DictionarieUtils.init(dictionariesPO);
        boolean insert = dictionariesService.insert(init);
        return AjaxResult.ok();
    }

    /**
     * 修改提交
     * @param dictionariesPO
     * @return
     */
    @PutMapping("/edit")
    @ResponseBody
    public AjaxResult edit(DictionariesPO dictionariesPO){
        if(dictionariesPO.getId()==null){
            return AjaxResult.error("没有id");
        }
        boolean b = dictionariesService.updateById(dictionariesPO);
        return AjaxResult.ok();
    }

    /**
     * 删除提交
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ResponseBody
    public AjaxResult delte(@RequestParam("id")Integer id){
        boolean b = dictionariesService.deleteById(id);
        return AjaxResult.ok();
    }

    /**
     * 打开选择父字典页面
     * @return
     */
    @GetMapping("/parent")
    public String parent(){
        return "page/dictionaries/parent";
    }
}
